﻿using System.Collections.Generic;
using UnityEngine;

public class PoolManager_Ki : MonoBehaviour
{
    private Dictionary<string, List<GameObject>> gameObjectsPool;
    private void Awake()
    {
        gameObjectsPool = new Dictionary<string, List<GameObject>>();
    }

    /*Temp
    //清理所有的内存
    public void ClearAll()
    {
        gameObjectsPool.Clear();
        //gameObjectsPool.Remove()  后期
    }
    */


    /// <summary>
    /// 让实例化过的物体重新回到场景
    /// </summary>
    /// <param name="Start_pos">重新生成物体的parents的开始位置</param>
    public GameObject ReturnScence(GameObject go_Prefab, Transform Start_pos = null)
    {
        string m_Key = go_Prefab.name + "(Clone)"; 
        GameObject ret = null;
         
        if (gameObjectsPool.ContainsKey(m_Key) && gameObjectsPool[m_Key].Count > 0){
            ret = gameObjectsPool[m_Key][0];     
            gameObjectsPool[m_Key].RemoveAt(0);  
            ret.SetActive(true);                
        }else{
            ret = GameObject.Instantiate(go_Prefab, Vector3.zero, Quaternion.identity);
        }
        if(Start_pos != null)
        {
            ret.transform.SetParent(Start_pos);
            ret.transform.localPosition = Vector3.zero;         
            ret.transform.localRotation = Quaternion.identity; 
        }

        return ret;
    }

    /// 隐藏掉一个物体
    public GameObject OutOfScene(GameObject pre_Clone)
    {

        if (gameObjectsPool.ContainsKey(pre_Clone.name))
        {
            gameObjectsPool[pre_Clone.name].Add(pre_Clone);
        }else{
            gameObjectsPool[pre_Clone.name] = new List<GameObject> { pre_Clone };
        }
       
        pre_Clone.SetActive(false);

        return pre_Clone;   //返回其实可以不需要，但是后期可能需要管理
    }

}
